# 物品路由
from flask import jsonify, request
from services.item_service import get_all_items, get_item_by_id, create_item, update_item, delete_item


def register_item_routes(app):
    @app.route('/items', methods=['GET'])
    def get_items():
        items = get_all_items()
        return jsonify([{
            'id': item.id,
            'name': item.name,
            'description': item.description,
            'price': item.price,
            'category': item.category,
            'stock': item.stock,
            'status': item.status,
            'user_id': item.user_id,
            'created_at': item.created_at.isoformat()
        } for item in items])
        
    @app.route('/items/<int:item_id>', methods=['GET'])
    def get_item(item_id):
        item = get_item_by_id(item_id)
        if not item:
            return jsonify({'error': 'Item not found'}), 404
        return jsonify({
            'id': item.id,
            'name': item.name,
            'description': item.description,
            'price': item.price,
            'category': item.category,
            'stock': item.stock,
            'status': item.status,
            'user_id': item.user_id,
            'created_at': item.created_at.isoformat()
        })
        
    @app.route('/items', methods=['POST'])
    def add_item():
        data = request.get_json()
        if not data or 'name' not in data or 'price' not in data or 'user_id' not in data:
            return jsonify({'error': 'Name, price and user_id are required'}), 400
        
        try:
            new_item = create_item(
                data['name'], 
                data.get('description'), 
                data['price'], 
                data['user_id'],
                data.get('category'),
                data.get('stock', 0),
                data.get('status', '在售')
            )
            return jsonify({
                'id': new_item.id,
                'name': new_item.name,
                'description': new_item.description,
                'price': new_item.price,
                'category': new_item.category,
                'stock': new_item.stock,
                'status': new_item.status,
                'user_id': new_item.user_id,
                'created_at': new_item.created_at.isoformat()
            }), 201
        except Exception as e:
            return jsonify({'error': str(e)}), 400
            
    @app.route('/items/<int:item_id>', methods=['PUT'])
    def update_item_route(item_id):
        data = request.get_json()
        if not data:
            return jsonify({'error': 'No data provided'}), 400
            
        updated_item = update_item(
            item_id, 
            data.get('name'), 
            data.get('description'), 
            data.get('price'),
            data.get('category'),
            data.get('stock'),
            data.get('status')
        )
        if not updated_item:
            return jsonify({'error': 'Item not found'}), 404
            
        return jsonify({
            'id': updated_item.id,
            'name': updated_item.name,
            'description': updated_item.description,
            'price': updated_item.price,
            'category': updated_item.category,
            'stock': updated_item.stock,
            'status': updated_item.status,
            'user_id': updated_item.user_id,
            'created_at': updated_item.created_at.isoformat()
        })
        
    @app.route('/items/<int:item_id>', methods=['DELETE'])
    def delete_item_route(item_id):
        success = delete_item(item_id)
        if not success:
            return jsonify({'error': 'Item not found'}), 404
        return jsonify({'message': 'Item deleted successfully'}), 200